//作废 表单
import { Tooltip, Button ,Input,Form, Col, Row, Select, Checkbox} from 'antd';
import eventBus, { useSubscribe } from "../../../framework/common/eventBus";

//几个审批类型公用的字段
import getInitFormData from './getInitFormData';//获取初始化表单数据
import InitiaterOrg from "../components/InitiaterOrg";//发起组织
import Opinion from "../components/Opinion";//审批意见
import CClist from "../components/CClist";//抄送人
import SignField from "../components/SignField";//签章

//作废表单
export default function({
    tplData,
    urlPrefix,
    formRef,
    approveType="Cancel",
    approveText
}){
    const [form] = Form.useForm();//表单对象
    formRef.current = form;//穿透form对象给父组件

    //表单默认值
    if(!tplData.__approveForm__[approveType]){
        tplData.__approveForm__[approveType] = getInitFormData({
            tplData: tplData,
            fields: {
                opinion: '',//审批意见
                initiater_org_id: '',//审批组织id
                initiater_org_name:'',//审批组织中文名
                step_cc_list: [],//抄送人
                sign_id: tplData.ProfileSign?.default_sign?.id || "" //签章
            }
        })
    }

    //设置发起表单数据，用于ant表单更新后，反向更新tplData内发起表单的数据
    function updateTplData(data){
        tplData.__approveForm__[approveType] = {
            ...tplData.__approveForm__[approveType], 
            ...data
        };
    }

    //console.log(approveType + "初始化表单数据",tplData.__approveForm__[approveType])

    return <Form
                form={form}
                name="basic"
                labelAlign={"left"}
                colon={false}
                labelCol={{
                    flex: "80px",
                }}
                onValuesChange={(value,allValues)=>{
                    //回填表单数据到tplData
                    //比较特殊需要额外回填的，单独到组件内的change事件处理，比如发起组织
                    updateTplData(allValues);
                }}
                initialValues={tplData.__approveForm__[approveType]}
                autoComplete="off"
            >
                <Form.Item
                    label={approveText + "意见"}
                    name="opinion"
                    rules={[
                        {
                            required: true,
                            message: '',
                        }
                    ]}
                >
                    <Opinion approveType={approveType} placeholder={"请输入" + approveText + "意见"}></Opinion>
                </Form.Item>
                <Form.Item
                    className="mb-10px"
                    label="审批组织"
                    name="initiater_org_id"
                    rules={[
                        {
                            required: true,
                            message: '',
                        },
                    ]}
                >
                    <InitiaterOrg
                        approveType={approveType}
                        form={form}
                        updateTplData={updateTplData}
                    ></InitiaterOrg>
                </Form.Item>
                {
                    tplData.show_step_cc === true
                    && <Form.Item
                            className="mb-10px"
                            label="抄送人"
                            name="step_cc_list"
                        >
                            <CClist
                                tplData={tplData}
                                urlPrefix={urlPrefix}
                                placeholder={"请选择抄送人"}
                            ></CClist>
                        </Form.Item>
                }
                {
                    (tplData.ProfileSign?.enable_sign == 1)
                    && <Form.Item
                        className="mb-0"
                        label="选择签章"
                        name="sign_id"
                        rules={[
                            {
                                required: true,
                                message: '',
                            }
                        ]}
                    >
                        <SignField 
                            tplData={tplData}
                        >
                        </SignField>
                    </Form.Item>
                }
            </Form>
}